<?php
/**
 * Created by PhpStorm.
 * User: JX
 * Date: 2017/9/7
 * Time: 13:14
 */

namespace App\Http\Controllers\API\WeChat;


use App\Http\Controllers\Controller;
use App\Models\WeChat;
use App\Models\WechatMassLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MassLogController extends Controller
{
//    public function getMasses(){
//        $wechats = WeChat::query()->get();
//        $begin = date('Y-m-01', strtotime(date("Y-m-d")));
//        $end = date('Y-m-d',strtotime("$begin +1 month -1 day"));
////        $begin =strtotime($begin);
////        dd(date('Y-m-d',$begin),date('Y-m-d',$end));
//        $res=[];
//        foreach ($wechats as $item){
//           $number =  DB::table('wechat_mass_logs')
//               ->where(['w_id'=>$item->id,'check_status'=>2])
//               ->whereBetween('created_at', [$begin, $end ])
//               ->count();
//           $items['name']=$item->nick_name;
//           $items['id']=(string)$item->id;
//           $items['number'] = 4-$number<0?0: 4-$number;
//           $res[]=$items;
//        }
//        return $res;
//    }
    public function index(Request $request)
    {
        $queryString = $request->get('query', '');
        $models = WechatMassLog::with(['Wechats'])
            ->whereHas('Wechats', function ($query) use ($queryString) {
                $query->where('nick_name', 'like', '%' . $queryString . '%');
            })
            ->orderBy('id','desc')
            ->paginate();
        return $models;
    }
    /**
     * 添加科室
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $model = new WechatMassLog();
        $model->fill($request->all());
        if ($model->save()) {
            return $model;
        } else {
            return response($model->getErrors()->toArray(), 406);
        }
    }


    /**
     * 修改员工
     * @param User $User
     * @param Request $request
     * @return mixed
     */
    public function update($id, Request $request)
    {
        $WechatMassLog = $this->findModel($id);
        $WechatMassLog->fill($request->all());
        if ($WechatMassLog->save()) {
            return $WechatMassLog;
        } else {
            return response($WechatMassLog->getErrors()->toArray(), 406);
        }
    }


    /**
     * 删除
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        $model = $this->findModel($id);
        try {
            $model->delete();
            return ['state' => 'success'];
        } catch (\Exception $e) {
            return ['state' => 'failed', 'message' => $e->getMessage()];
        }
    }


    /**
     * 返回单个实例
     * @param User $user
     * @return mixed
     */
    public function show($id)
    {
        return $this->findModel($id);
    }




    /**
     * @param $id
     * @return Staff
     */
    public function findModel($id)
    {
        $model =  WechatMassLog::with(['Wechats'])->findOrFail($id);
        return $model;
    }
}